package com.example.shop.shops.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shop.base.config.BussinessException;
import com.example.shop.shops.mapper.EmployeeMapper;
import com.example.shop.ov.AdminOv;
import com.example.shop.ov.AuditShopDto;
import com.example.shop.ov.ShopOv;
import com.example.shop.shops.domain.Employee;
import com.example.shop.shops.domain.Log;
import com.example.shop.shops.domain.Shop;
import com.example.shop.shops.domain.ShopDto;
import com.example.shop.shops.mapper.LogMapper;
import com.example.shop.shops.mapper.ShopMapper;
import com.example.shop.shops.service.ShopService;
import com.example.shop.util.BaiduAuditUtils;
import com.example.shop.util.BasePage;
import com.example.shop.util.MailUtil;
import com.example.shop.util.PageList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Hasee
 * @description 针对表【t_shop】的数据库操作Service实现
 * @createDate 2022-11-21 12:21:46
 */
@Service
@Slf4j
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogMapper logMapper;

    @Async(value = "taskExecutor")
    @Override
    public void add(ShopOv shopOv) {
        this.eqs(shopOv);
        this.save(shopOv);
    }

    @Override
    public PageList<Shop> getPageList(BasePage basePage) {
        PageList<Shop> shopPageList = new PageList<>();
        Page<Shop> page = new Page<>(basePage.getCurrentPage(), basePage.getPageSize());
        QueryWrapper<Shop> shopQueryWrapper = new QueryWrapper<>();
        Page<Shop> page1 = baseMapper.selectPage(page, shopQueryWrapper);
        List<Shop> records = page1.getRecords();
        shopPageList.setTotals((int) page1.getTotal());
        shopPageList.setRows(records);
        return shopPageList;
    }

    @Async(value = "taskExecutor")
    @Override
    public void auditPass(AuditShopDto dto) {
        // 1.更改 店铺的状态为 待激活
        Shop shop = new Shop();
        shop.setId(dto.getShopId());
        shop.setState(1);
        baseMapper.updateById(shop);
        // 2.添加审核日志
        Log logs = new Log();
        // todo 因为现在我们还没有做登陆 所以先写死一个id
        logs.setState(4);
        logs.setAuditId(1L);
        logs.setAuditTime(new Date());
        logs.setShopId(dto.getShopId());
        logs.setNote(dto.getNote());
        logMapper.insert(logs);
        // 成功消息
        String succeed = "<h1>亲爱的用户：恭喜你入住我平台店铺，审核结果为：通过。请点击下方链接激活店铺：</h1>";
        String url = "http://localhost:8080/shop/active/";
        MailUtil.getMaiUtil("2949163740@qq.com", "2949163740@qq.com", dto.getShopId(), succeed, url);
    }

    @Async(value = "taskExecutor")
    @Override
    public void active(Long id) {
        Shop shop = new Shop();
        shop.setId(id);
        shop.setState(3);
        baseMapper.updateById(shop);
    }

    @Async(value = "taskExecutor")
    @Override
    public void auditReject(AuditShopDto dto) {
        // 1.修改店铺状态为 驳回
        Shop shop = new Shop();
        shop.setId(dto.getShopId());
        shop.setState(0);
        baseMapper.updateById(shop);
        // 2.添加审核日志
        Log logs = new Log();
        logs.setAuditId(2L);
        logs.setState(4);
        logs.setAuditTime(new Date());
        logs.setShopId(dto.getShopId());
        logs.setNote(dto.getNote());
        logMapper.insert(logs);
        String succeed = "<h1>亲爱的用户：恭喜你入住我平台店铺，审核结果为：不通过。请点击下方链接重新入住店铺：</h1> ";
        String url = "http://localhost:8081/#/shop?shopId=";
        MailUtil.getMaiUtil("2949163740@qq.com", "2949163740@qq.com", dto.getShopId(), succeed, url);
    }

    @Override
    public PageList<ShopDto> getJon(BasePage basePage) {
//        List<ShopDto> jon = baseMapper.getJon(basePage);
        //        shopPageList.setRows(jon);
//        Integer remunerate = baseMapper.remunerate(basePage);
//        shopPageList.setTotals(remunerate);
        return new PageList<>();
    }

    private void eqs(ShopOv shopOv) {
        AdminOv admin = shopOv.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BussinessException("两次密码不一致");
        }
        Shop shop = baseMapper.selectOne(new QueryWrapper<Shop>().eq("name", shopOv.getName()));
        if (null != shop) {
            throw new BussinessException("店铺已经存在");
        }
        Employee emp = shopOv.getEmp();
        Employee name = employeeMapper.selectOne(new QueryWrapper<Employee>().eq("username", emp.getUsername()));
        if (null != name && null != name.getShopId()) {
            shop = baseMapper.selectOne(new QueryWrapper<Shop>().eq("admin_id", name.getId()));
            if (null != shop) {
                throw new BussinessException("你已经管理了店铺了！");
            }
        }
        if (!BaiduAuditUtils.TextCensor(shopOv.getName())) {
            throw new BussinessException("你的店铺名称违法");
        }
        if (!BaiduAuditUtils.ImgCensor(shopOv.getLogo())) {
            throw new BussinessException("店铺logo非法！");
        }
    }

    private void save(ShopOv shopOv) {
        // 保存店铺数据
        Shop insertShop = shopOv.getShop();
        baseMapper.insert(insertShop);
        // 保存员工数据
        Employee emp = shopOv.getEmp();
        emp.setShopId(insertShop.getId());
        employeeMapper.insert(emp);
        // 修改店铺关联的店长id
        Shop updateShop = new Shop();
        updateShop.setId(insertShop.getId());
        updateShop.setAdminId(emp.getId());
        System.out.println(updateShop);
        baseMapper.updateById(updateShop);
    }
}
